package org.stars.dao.loader;

import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.stars.config.Message;
import org.stars.dao.config.TypeAlias;
import org.stars.dao.config.TypeAliases;
import org.stars.dao.exception.parser.TypeAliasException;
import org.stars.datastructure.map.CaseInsensitiveHashMap;

public class TypeAliasLoader {
	protected static Log log = LogFactory.getLog(SourceLoader.class);

	protected static Map<String, TypeAlias> load(TypeAliases typeAliases) {
		log.debug(Message.TYPE_ALIAS_LOADER_01.toString());
		Map<String, TypeAlias> map = new CaseInsensitiveHashMap<TypeAlias>();
		String msg;

		for (TypeAlias item : typeAliases.getList()) {
			try {
				Class.forName(item.getClassName()).newInstance();
				msg = Message.TYPE_ALIAS_LOADER_03.with(item.getId(),
						item.getClassName());
				log.info(msg);
				map.put(item.getId(), item);
			} catch (Exception e) {
				msg = Message.TYPE_ALIAS_ERROR.with(item.getId(),
						item.getClassName());
				log.error(msg);
				throw (new TypeAliasException(msg));
			}
		}

		log.debug(Message.TYPE_ALIAS_LOADER_02.toString());
		return map;

	}
}
